import {login, logout, getUserInfo} from '@/api/user'
import {menuRoutes} from '@/api/sys/menu'
import { setToken, getToken, localSave } from '@/libs/util'
import { encrypt } from '@/libs/tools'

export default {
    state: {
        info: {},
        token: '',
        access: '',
        hasGetInfo: false,
        loadedPermission: false,
        unreadCount: 0,
        messageUnreadList: [],
        messageReadedList: [],
        messageTrashList: [],
        messageContentStore: {}
    },
    mutations: {
        setUser (state, info) {
            state.info = info
        },
        setPermission(state, data){
            state.permission = data
        },
        setAccess (state, access) {
            state.access = access
        },
        setToken (state, token) {
            state.token = token;
            setToken(token)
        },
        setHasGetInfo (state, status) {
            state.hasGetInfo = status
        },
        loadPermission(state, status){
            state.loadedPermission = status;
        }
    },
    getters: {
        getUser: state => state.info
    },
    actions: {
        // 登录
        handleLogin ({ commit }, {username, password, source_from, self}) {
            return new Promise((resolve, reject) => {
                login({username, password, source_from}).then((res) => {
                    if(res.code && res.code !== 0){
                        resolve(false);
                        self.$message({
                            message: res.msg,
                            duration: 3000,
                            type: 'warning'
                        });
                    }else{
                        commit('setToken', res.data.token);
                        resolve(true);
                    }
                }).catch(err => {
                    reject(err)
                })
            })
        },
        // 退出登录
        handleLogOut ({ state, commit }) {
            return new Promise((resolve, reject) => {
                logout().then((res) => {
                    commit('setToken', '');
                    commit('setAccess', '');
                    commit('setHasGetInfo', false);
                    commit('setUser', null);
                    resolve(res);
                }).catch(err => {
                    reject(err)
                });
            })
        },
        // 获取用户相关信息
        getUserInfo ({ state, commit },self) {
            return new Promise((resolve, reject) => {
                getUserInfo().then((res) => {
                    if(res.data.id && res.data.id > 0){
                        localSave('userInfo', JSON.stringify(res.data));
                        commit('setUser', res.data);
                        commit('setAccess', state.token);
                        commit('setHasGetInfo', true);
                        resolve(true);
                    }else{
                        self.$message({
                            message: "登陆失败,请重启登陆",
                            duration: 3000,
                            type: 'warning'
                        });
                        resolve(false);
                    }
                });
            })
        },
        //获取权限路由
        getPermission({ state, commit }) {
            return new Promise((resolve, reject) => {
              menuRoutes().then(res => {
                    if (res.code === 0 ) {
                        commit('setPermission', res.data);
                    }
                    resolve(res);
                }).catch(err => {
                    reject(err);
                });
            });
        },
    }
}
